package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserToken;
import com.alan.libapi.model.persistent.ModifyPwdType;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.utils.BusinessUtil;
import com.alan.wifishare.view.iview.IVUserVerifyIdentity;

/**
 * @className: UserVerifyIdentityPS
 * @describtion: UserVerifyIdentityPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/5 19:00
 */
public class UserVerifyIdentityPS {

    IVUserVerifyIdentity ivUserVerifyIdentity;

    public UserVerifyIdentityPS(IVUserVerifyIdentity ivUserVerifyIdentity) {
        this.ivUserVerifyIdentity = ivUserVerifyIdentity;
    }

    /**
     * 提交手机号请求服务器发送验证码
     */
    public void requestVerifyCode() {
        String mobilePhone = ivUserVerifyIdentity.getUserPhone();
        if(!BusinessUtil.isMobileNum(mobilePhone)){
            ivUserVerifyIdentity.toastMsg("手机号输入错误");
            return;
        }
        // TODO: 对手机号加密

        ModifyPwdType typeTag = ivUserVerifyIdentity.getModifyType();
        GlobalField.businessHttp.postPhoneToModifyPassword(typeTag, mobilePhone, new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivUserVerifyIdentity.getVerifyCodeSuccess();
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivUserVerifyIdentity.toastMsg(msg);
                    }
                });
            }
        });
    }

    public void sendVerifyCodeToServer(){
        ModifyPwdType typeTag = ivUserVerifyIdentity.getModifyType();
        String userPhone = ivUserVerifyIdentity.getUserPhone();
        String verifyCode = ivUserVerifyIdentity.getVerifyCode();
        if(!BusinessUtil.isMobileNum(userPhone)){
            ivUserVerifyIdentity.toastMsg("手机号输入错误");
            return;
        }
        if(TextUtils.isEmpty(verifyCode)){
            ivUserVerifyIdentity.toastMsg("验证码不能为空");
            return;
        }
        ivUserVerifyIdentity.showProgress();
        GlobalField.businessHttp.postVerifyCodeToModifyPassword(typeTag, userPhone, verifyCode, new CallbackListener<UserToken>() {
            @Override
            public void onSuccess(final UserToken result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivUserVerifyIdentity.hideProgress();
                        ivUserVerifyIdentity.verifySuccess(result);
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivUserVerifyIdentity.toastMsg(msg);
                        ivUserVerifyIdentity.hideProgress();
                    }
                });
            }
        });
    }
}
